package com.dunshan.prometh.controller;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author dunshan
 * @program: dunshan-pef
 * @description: 自定义下单接口采集类
 * @date 2021-09-25 10:13:46
 */
@Component
public class PrometheusCustomMonitor {

    /**
     * 下单次数
     */
    private Counter orderCount;
    /**
     * 下单失败数
     */
    private Counter failureCount;
    /**
     * 金额统计
     */
    private DistributionSummary amountSum;


    /**
     * 影子表订单数据
     */
    private Counter shadowOrderCount;

    /**
     * 影子表下单失败数
     */
    private Counter shadowFailureCount;
    /**
     * 影子表金额统计
     */
    private DistributionSummary shadowAmountSum;


    /**
     * Micrometer是一个抽象类
     */
    private final MeterRegistry registry;



    @Autowired
    public PrometheusCustomMonitor(MeterRegistry registry) {
        this.registry = registry;
    }

    @PostConstruct
    private void init() {
        //统计下单数
        orderCount = registry.counter("order_request_count", "order", "mall-order");
        //统计失败数
        failureCount = registry.counter("order_failure_count", "order", "mall-order");
        //统计金额数据
        amountSum = registry.summary("order_amount_sum", "orderAmount", "mall-order");

        //影子表统计下单数
        shadowOrderCount = registry.counter("shadow_order_request_count", "order", "mall-order");
        //影子表统计失败数
        shadowFailureCount = registry.counter("shadow_order_failure_count", "order", "mall-order");
        //影子表统计金额数据
        shadowAmountSum = registry.summary("shadow_order_amount_sum", "orderAmount", "mall-order");


    }
    public Counter getOrderCount() {
        return orderCount;
    }

    public Counter getFailureCount() {
        return failureCount;
    }

    public DistributionSummary getAmountSum() {
        return amountSum;
    }

    public Counter getShadowOrderCount() {
        return shadowOrderCount;
    }

    public Counter getShadowFailureCount() {
        return shadowFailureCount;
    }

    public DistributionSummary getShadowAmountSum() {
        return shadowAmountSum;
    }
}
